﻿using System;
using System.Collections.Generic;
using Gp.Scripts.Core;
using Gp.Scripts.Core.Res;
using Gp.Scripts.Core.Skill;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.GUI
{
    public class BattleUI3DNode
    {
        public Transform Root { get; private set; }
        private Dictionary<int, BattleIndicatorBase> _map;

        private BattleIndicatorBase _temp = null;
        private ResLoader _resLoader = null;
        private SkillAreaIndicator _skillAreaIndicator;

        public void OnInit(Transform root)
        {
            Root       = root;
            _map       = new Dictionary<int, BattleIndicatorBase>();
            _resLoader = ResLoader.Allocate();

            // 载入范围指示
            var rangeDrawer = _resLoader.LoadSync<GameObject>(ConstBundle.BATTLE, "SKillRangeDrawer");
            _skillAreaIndicator = rangeDrawer.InstantiateWithParent(Root).GetComponent<SkillAreaIndicator>();
        }


        public BattleIndicatorBase GetIndicator(ConfigSkill config)
        {
            if (_temp != null) return _temp;

            _temp = new MoveAndAttackIndicator();
            _temp.Init(CreateFromPrefab("MoveSkillIndicator"), this);

            return _temp;
        }


        private Transform CreateFromPrefab(string prefabName)
        {
            var instance = _resLoader.LoadSync<Transform>(ConstBundle.BATTLE, prefabName).Instantiate();
            instance.SetParent(Root);
            return instance;
        }

        public void OnDestroy()
        {
            Root.gameObject.SetActive(false);
            _resLoader.Recycle2Cache();
        }

        public void ShowSkillRange(IEnumerable<VBattleCell> cells)
        {
            this._skillAreaIndicator.DrawCells(cells);
        }

        public void CleanUpSkillRange()
        {
            _skillAreaIndicator.ClearAll();
        }
    }
}